﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace TA.DotNetUtils
{
	public class IdValue<I, T> : SimpleIdValue<I, T>
		where I : struct
		where T : IHasId<I>, new()
	{
		
		T _leanValue;

		public IdValue(I id):base(id)
		{
		}
		public IdValue(T value):base(value)
		{
		}
		public T LeanValue
		{
			get
			{
				if (_leanValue == null)
				{
					_leanValue = new T { GenericId = Id };
				}
				return _leanValue;
			}
			set
			{
				Id = (I)value.GenericId;
			}
		}
		public static implicit operator IdValue<I, T>(I id)
		{
			return new IdValue<I, T>(id);
		}
		public static implicit operator IdValue<I, T>(T value)
		{
			if (value == null)
				return null;
			return new IdValue<I, T>(value);
		}
		public static implicit operator I(IdValue<I, T> idValue)
		{
			if (idValue == null) return default(I);
			return idValue.Id;
		}
		public static implicit operator I?(IdValue<I, T> idValue)
		{
			if (idValue == null) return null;
			return idValue.Id;
		}
	}
}
